import { stringify } from 'querystring';
import { history } from 'umi';
import {
    fetchListDepartment,
    fakeUpdateDepartment,
    fakeInsertDepartment,
    fakeDeleteDepartment,
    fetchSearchAdmin,
    fetchRole,
    fetchRoleDetail,
    fakeDeleteRoleAdmin,
    fetchExportAdmin,
    fetchImportAdminExcelTemplate,
    fakeSendSmsCodeBack,
    fakeInsertAuthRole,
    fakeUpdateAuthRole,
    fakeInsertAuthAdmin,
    fakeUpdateAuthAdmin,
    fakeEnableAdmin,
    fakeDisableAdmin,
    fetchAdminDetail,
    fetchDeleteAuthRole,
    fetchDeleteRoleAdmin
} from '@/services/SystemServices';
import { notification } from 'antd';
const LoginForm = {
    namespace: 'system',
    state: {
        authInfo: [],
        departmentList: [],
        departmentNewList: [],
        list: {
            list: [],
            total: 0,
        },
        organizationAuthAdminList: {
            list: [],
            total: 0,
        },
    },

    effects: {
        *fetchDeleteAuthRole({ payload, callback }, { call, put }) {
            const response = yield call(fetchDeleteAuthRole, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '删除成功',
            });
            callback && callback(content);
        },
        *fetchDeleteRoleAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fetchDeleteRoleAdmin, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '删除成功',
            });
            callback && callback(content);
        },
        *fakeEnableAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fakeEnableAdmin, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '启用成功',
            });
            callback && callback(content);
        },
        *fakeDisableAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fakeDisableAdmin, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '禁用成功',
            });
            callback && callback(content);
        },
        *fetchAdminDetail({ payload, callback }, { call, put }) {
            const response = yield call(fetchAdminDetail, payload);
            if (!response) return;
            callback && callback(response.content);
        },
        *fakeInsertAuthAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fakeInsertAuthAdmin, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '新增成功',
            });
            callback && callback(content);
        },
        *fakeUpdateAuthAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fakeUpdateAuthAdmin, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '修改成功',
            });
            callback && callback(content);
        },
        *fakeInsertAuthRole({ payload, callback }, { call, put }) {
            const response = yield call(fakeInsertAuthRole, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '新增成功',
            });
            callback && callback(content);
        },
        *fakeUpdateAuthRole({ payload, callback }, { call, put }) {
            const response = yield call(fakeUpdateAuthRole, payload);
            if (!response) return;
            const { content } = response;
            notification.success({
                message: '温馨提示',
                description: '修改成功',
            });
            callback && callback(content);
        },
        *fakeSendSmsCodeBack({ payload, callback }, { call, put }) {
            const response = yield call(fakeSendSmsCodeBack, payload);
            if (!response) return;
            callback && callback(response.content);
        },

        *fetchExportAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fetchExportAdmin, payload);
            if (!response) return;
            const { content } = response;
            callback && callback(content);
        },
        *fetchImportAdminExcelTemplate({ payload, callback }, { call, put }) {
            const response = yield call(fetchImportAdminExcelTemplate, payload);
            if (!response) return;
            const { content } = response;
            callback && callback(content);
        },
        *fetchListDepartment({ payload, callback }, { call, put }) {
            const response = yield call(fetchListDepartment, payload);
            if (!response) return;
            const {
                content: { departmentList },
            } = response;
            yield put({
                type: 'save',
                payload: {
                    departmentNewList: departmentList,
                },
            });
        },
        *fakeUpdateDepartment({ payload, callback }, { call, put }) {
            const response = yield call(fakeUpdateDepartment, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: '修改部门',
            });
            callback && callback();
        },
        *fakeInsertDepartment({ payload, callback }, { call, put }) {
            const response = yield call(fakeInsertDepartment, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: '新增部门',
            });
            callback && callback();
        },
        *fakeDeleteDepartment({ payload, callback }, { call, put }) {
            const response = yield call(fakeDeleteDepartment, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: '删除部门',
            });
            callback && callback();
        },

        *fetchSearchAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fetchSearchAdmin, {
                status: '1',
                ...payload,
            });
            if (!response) return;
            const { authAdminList, total } = response.content;
            yield put({
                type: 'save',
                payload: {
                    organizationAuthAdminList: {
                        list: authAdminList,
                        total,
                    },
                },
            });
            callback && callback(authAdminList);
        },
        *fetchRole({ payload, callback }, { call, put }) {
            const response = yield call(fetchRole, payload);
            if (!response) return;
            const { content } = response;
            callback && callback(content);
        },
        *fetchRoleDetail({ payload, callback }, { call, put }) {
            const response = yield call(fetchRoleDetail, payload);
            if (!response) return;
            const { content } = response;
            callback && callback(content);
        },
        *fakeDeleteRoleAdmin({ payload, callback }, { call, put }) {
            const response = yield call(fakeDeleteRoleAdmin, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: '删除成功',
            });
            callback && callback();
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};
export default LoginForm;
